<?php

namespace Rebecca\RebeccaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Rebecca\RebeccaBundle\Entity\FondoPensiones;
use Rebecca\RebeccaBundle\Form\FondoPensionesType;

/**
 * FondoPensiones controller.
 *
 */
class FondoPensionesController extends Controller
{
    /**
     * Lists all FondoPensiones entities.
     *
     */
    public function indexAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\FondoPensionesComponent */
        $component = $this->get("components.FondoPensiones");
        $entities = $component->getAll();

        return $this->render('RebeccaBundle:FondoPensiones:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a FondoPensiones entity.
     *
     */
    public function showAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\FondoPensionesComponent */
        $component = $this->get("components.FondoPensiones");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find FondoPensiones entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:FondoPensiones:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }
    
    /**
     * Lists all payments of Salud entities.
     *
     */
    public function paymentsAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\SeguridadSocialComponent */
        $component = $this->get("components.SeguridadSocial");
        $periodos = $component->getPeriodosLiquidados();
        
        $idPeriodo = $this->getRequest()->get("idPeriodo");
        
        $entities = null;
        
        if(isset ($idPeriodo)){
            $entities = $component->getPagos($idPeriodo, '11');
        }
        
        $returnPath = $this->generateUrl("fondopensiones");
        $path = $this->generateUrl("fondopensiones_payments");

        return $this->render('RebeccaBundle:SeguridadSocial:pagos.html.twig', array(
            'periodos'  => $periodos,
            'entities'  => $entities,
            'idPeriodo' => $idPeriodo,
            'return'    => $returnPath,
            'action'    => $path,
            'title'     => 'Fondos de Pensiones'
        ));
    }

    /**
     * Displays a form to create a new FondoPensiones entity.
     *
     */
    public function newAction()
    {
        $entity = new FondoPensiones();
        $form   = $this->createForm(new FondoPensionesType(), $entity);

        return $this->render('RebeccaBundle:FondoPensiones:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Creates a new FondoPensiones entity.
     *
     */
    public function createAction()
    {
        $entity  = new FondoPensiones();
        $request = $this->getRequest();
        $form    = $this->createForm(new FondoPensionesType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\FondoPensionesComponent */
            $component = $this->get("components.FondoPensiones");
            $component->save($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('fondopensiones'));
            
        }

        return $this->render('RebeccaBundle:FondoPensiones:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Displays a form to edit an existing FondoPensiones entity.
     *
     */
    public function editAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\FondoPensionesComponent */
        $component = $this->get("components.FondoPensiones");
        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find FondoPensiones entity.');
        }

        $editForm = $this->createForm(new FondoPensionesType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:FondoPensiones:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing FondoPensiones entity.
     *
     */
    public function updateAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\FondoPensionesComponent */
        $component = $this->get("components.FondoPensiones");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find FondoPensiones entity.');
        }

        $editForm   = $this->createForm(new FondoPensionesType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $component->update($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('fondopensiones'));
        }

        return $this->render('RebeccaBundle:FondoPensiones:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a FondoPensiones entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\FondoPensionesComponent */
            $component = $this->get("components.FondoPensiones");
            $entity = $component->get($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find FondoPensiones entity.');
            }

            $component->delete($entity);
            $component->commit();
        }

        return $this->redirect($this->generateUrl('fondopensiones'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
